<?php

declare(strict_types=1);

namespace App\Controller\order;

use App\Common\Common;
use App\Common\User;
use App\Controller\AbstractController;
use Hyperf\Context\Context;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\AutoController;
use Swoole\Coroutine\Channel;
use function Hyperf\Config\config;
use function Hyperf\Coroutine\co;
use function Hyperf\Support\today;

#[AutoController(prefix: "orderfeed")]
class OrderfeedController extends AbstractController
{

    /**
     * 提交订单反馈申请
     * @return null
     */
    public function apply()
    {
        $param = $this->request->all();
        $validator = $this->validatorFactory->make(
            $param,
            [
                'uid' => 'required',
                'type' => 'required',
                'ordersn' => 'required',
                'images' => 'required',
                //'upi_utr' => 'required',
            ],
            [
                'uid.required' => 'uid is required',
                'type.required' => 'type is required',
                'ordersn.required' => 'ordersn is required',
                'images.required' => 'images is required',
                //'upi_utr.required' => 'upi_utr is required',
            ]
        );
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            $this->logger->error("订单反馈申请数据验证失败===>".$errorMessage);
            return $this->ReturnJson->failFul(219,[]);
        }

        $order_feed = Db::table('order_feedback')->where('ordersn',$param['ordersn'])->whereIn('status',[0,1,2])->first();
        if (!empty($order_feed)){
            return $this->ReturnJson->failFul(235,[]);
        }
        if ($param['type'] == 1) {
            $order = Db::table('order')
                ->where('ordersn', $param['ordersn'])
                ->selectRaw('ordersn,paytype')
                ->first();
        }else{
            $order = Db::table('withdraw_log')
                ->where('ordersn', $param['ordersn'])
                ->selectRaw('ordersn,withdraw_type as paytype')
                ->first();
        }
        if (empty($order)){
            return $this->ReturnJson->failFul(235,[]);
        }

        try {
            Db::beginTransaction();

            $res = Db::table('order_feedback')->insert([
                'uid' => $param['uid'],
                'ordersn' => $order['ordersn'],
                'paytype' => $order['paytype'],
                'type' => $param['type'],
                'images' => $param['images'],
                'createtime' => time(),
                //'upi_utr' => $param['upi_utr'],
            ]);

            Db::commit();
            return $this->ReturnJson->successFul(200,[],1);

        }catch (\Throwable $e){
            Db::rollback();
            $this->logger->error("错误文件===" . $e->getFile() . '===错误行数===' . $e->getLine() . '===错误信息===' . $e->getMessage());
            return $this->ReturnJson->failFul();
        }

    }

    /**
     * 用户反馈
     * @return null
     */
    public function userFeed()
    {
        $param = $this->request->all();
        $validator = $this->validatorFactory->make(
            $param,
            [
                'uid' => 'required',
                'content' => 'required',
                'images' => 'required',
                //'upi_utr' => 'required',
            ],
            [
                'uid.required' => 'uid is required',
                'content.required' => 'content is required',
                'images.required' => 'images is required',
                //'upi_utr.required' => 'upi_utr is required',
            ]
        );
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            $this->logger->error("用户反馈申请数据验证失败===>".$errorMessage);
            return $this->ReturnJson->failFul(219,[]);
        }

        try {
            Db::beginTransaction();

            $res = Db::table('feedback')->insert([
                'uid' => $param['uid'],
                'content' => $param['content'],
                'images' => $param['images'],
                'createtime' => time(),
                //'upi_utr' => $param['upi_utr'],
            ]);

            Db::commit();
            return $this->ReturnJson->successFul(200,[],1);

        }catch (\Throwable $e){
            Db::rollback();
            $this->logger->error("错误文件===" . $e->getFile() . '===错误行数===' . $e->getLine() . '===错误信息===' . $e->getMessage());
            return $this->ReturnJson->failFul();
        }

    }



}
